%% Our HW problem to find prime numbers


%Number of prime numbers to find
%It's better to use a variable than to 'hard code' a number everywhere in
%the code
Nmax=10;

%make an empty array to hold the first
primes=zeros(Nmax,1)

%Counter for number we have found
Nprime=0

%Our current integer to be tested
Ntest=2

while Nprime<Nmax %don't stop until we find Nmax primes
    
    %set a flag 
    notPrime=0;
    for ii=2:Ntest-1 %test all integers up to Ntest-1 (we could also just go to sqrt(Ntest))
        if Ntest/ii==round(Ntest/ii)
            notPrime=1; %This means it is not a prime
        end
    end
    
    if notPrime==0 %this means it 'survived' the division test of all integers
        Nprime=Nprime+1;    %we found another, so increment our counter
        primes(Nprime)=Ntest; %put our found prime into the array
    end

    Ntest=Ntest+1; %increment the integer that we will test 
end

primes

%% The same code using a function


%Number of prime numbers to find
%It's better to use a variable than to 'hard code' a number everywhere in
%the code
Nmax=10;

%make an empty array to hold the first
primes=zeros(Nmax,1)

%Counter for number we have found
Nprime=0

%Our current integer to be tested
Ntest=2

while Nprime<Nmax %don't stop until we find Nmax primes
    
    %use a function to replace code.  This is easier to read and
    %understand. 
    [ notPrime ] = CheckPrime(Ntest)
    
    if notPrime==0 %this means it 'survived' the division test of all integers
        Nprime=Nprime+1;    %we found another, so increment our counter
        primes(Nprime)=Ntest; %put our found prime into the array
    end

    Ntest=Ntest+1; %increment the integer that we will test 
end

primes


%%  Structures

%Stuctures are a data type that can be used to organize multiple varialble,
%even when they are different types. 

% 'Data' is a structure

% Assign the element 'X' to 'Data'
Data.X=(1:10)   %(1:10) generates a row vector

%access the element like this:
x=Data.X

%we can store as many elements as we want in 'Data'
Data.Y=x.^2     %this is an array of double
Data.FileName='Experiment1'     %this is a character string
Data.LaserOn=1 %this is a double scalar

%see all of Data
Data

%It would be nice if 'Data' could plot its own data- X and Y.  
%This is something we can do with object oriented programming. 

%% Object Oriented Programming

%see the following for an entry point into matlab's documentation about 
%object oriented programing.  
% 'doc classdef'
%For now, it might be a little hard to follow until we get a bit 
%more experience. 

%Here is my short intro. We will build on this. 

% A 'class' has a predefined set of 'properties' and 'methods'
% These are defined in a separate 'classdef' file.  
% For this example see 'LaserData.m'

% An 'object' is an 'instance' of a class.  You can have multiple objects
% from the same class and they can have different values assigned to thier
% properties. 

% Make an object from class 'LaserData'
LD1=LaserData

%The 'properties' are like elements of a structure. We can assign and
%access them the same way. 

LD1.X
%This starts empty, but we can put something in there
LD1.X=(1:10)
%and now the other properties..
LD1.Y=(1:10).^2
LD1.LaserOn=1
LD1.FileName='Experiment1'

%Note: we can't add elements that aren't in the class definition
LD1.Z=5     % This will give an error

% Classes can  have 'methods' which are really just functions. 
% LaserData has a method called plotData

LD1.plotData()

% make another object of the class 'LaserData'
LD2=LaserData
LD2.X=(1:10)
LD2.Y=(1:10)
LD2.LaserOn=0
LD2.FileName='Experiment2'
LD2.plotData()






























